package com.ruiysoft.crm.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ruiysoft.crm.entity.Course;
import com.ruiysoft.crm.entity.CourseDetail;
import com.ruiysoft.crm.entity.EmployeeCourse;
import com.ruiysoft.crm.mapper.CourseDetailMapper;
import com.ruiysoft.crm.mapper.EmployeeCourseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Yanghu
 * @since 2018-08-21
 */
@RestController
@RequestMapping("courseDetail")
public class CourseDetailController {
    @Autowired
    CourseDetailMapper courseDetailMapper;

    @Autowired
    EmployeeCourseMapper employeeCourseMapper;

    @Cacheable("getCourseByCourseId")
    @GetMapping("getCourseByCourseId")
    public Object getCourseByCourseId(String id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);

        try {
            EntityWrapper<CourseDetail> entityWrapper = new EntityWrapper();
            entityWrapper.eq("courseId", id);
            entityWrapper.orderBy("sequence", true);
            List<CourseDetail> list = courseDetailMapper.selectList(entityWrapper);
            if(list.size() > 0) {
                map.put("success", true);
                map.put("data", list.get(0));
            }
            else
                map.put("errorMsg", "查询数据失败");

        } catch (Exception error) {
            map.put("errorMsg", "delete 异常: " + error.getMessage());
        }

        return map;
    }

    @PostMapping("create")
    public Object create(@RequestBody CourseDetail obj, String courseId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);

        try {
            obj.setCourseId(Integer.parseInt(courseId));
            obj.setCreateTime(new Date());
            obj.setUpdaetTime(new Date());

            if (courseDetailMapper.insert(obj) > 0)
                map.put("success", true);

        } catch (Exception error) {
            map.put("errorMsg", "create 异常: " + error.getMessage());
        }

        return map;
    }

    @GetMapping("delete")
    public Object delete(Integer id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);

        try {
            if (courseDetailMapper.deleteById(id) > 0)
                map.put("success", true);

        } catch (Exception error) {
            map.put("errorMsg", "delete 异常: " + error.getMessage());
        }

        return map;
    }

    @PostMapping("edit")
    public Object edit(@RequestBody CourseDetail obj) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);

        try {
            obj.setUpdaetTime(new Date());
            if (courseDetailMapper.updateById(obj) > 0)
                map.put("success", true);
            else
                map.put("errorMsg", "更新失败!");

        } catch (Exception error) {
            map.put("errorMsg", "create 异常: " + error.getMessage());
        }

        return map;
    }

    @Cacheable("getCourseDetailById")
    @GetMapping("getCourseDetailById")
    public Object getCourseDetailById(Integer id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);

        try {
            CourseDetail courseDetail = courseDetailMapper.selectById(id);
            if(courseDetail != null) {
                if(courseDetail.getPlayTime() != null)
                    courseDetail.setPlayTime(courseDetail.getPlayTime() + 1);
                else
                    courseDetail.setPlayTime(1);
                courseDetailMapper.updateById(courseDetail);

                map.put("success", true);
                map.put("data", courseDetail);
            }
            else
                map.put("errorMsg", "查询数据失败");

        } catch (Exception error) {
            map.put("errorMsg", "delete 异常: " + error.getMessage());
        }

        return map;
    }

    @Cacheable("getCourseListByCourseId")
    @GetMapping("getCourseListByCourseId")
    public Object getCourseListByCourseId(String courseId, String employeeId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);

        try {
            EntityWrapper<CourseDetail> entityWrapper = new EntityWrapper();
            entityWrapper.eq("courseId", courseId);
            entityWrapper.orderBy("sequence", true);
            List<CourseDetail> list = courseDetailMapper.selectList(entityWrapper);

            EmployeeCourse employeeCourse = new EmployeeCourse();
            employeeCourse.setCourseId(courseId + "");
            if(!employeeId.equals("null"))
                employeeCourse.setEmployeeId(Integer.parseInt(employeeId));
            boolean isPay = isBuyCourse(employeeCourse);

            if(list.size() > 0) {

                map.put("success", true);
                map.put("data", list);
                map.put("isPay", isPay);
            }
            else
                map.put("errorMsg", "查询数据失败");

        } catch (Exception error) {
            map.put("errorMsg", "delete 异常: " + error.getMessage());
        }

        return map;
    }

    boolean isBuyCourse(EmployeeCourse employeeCourse) {
        if(employeeCourseMapper.selectOne(employeeCourse) != null)
            return true;

        return false;
    }
}
